package cc.rengu.igas.bomp.core.service.trans;


import cc.rengu.igas.bomp.common.dao.MchntBaseInfoMapper;
import cc.rengu.igas.bomp.common.dao.TxnLimitCfgMapper;
import cc.rengu.igas.bomp.common.dao.impl.MchntBaseInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.TxnLimitCfgMapperImpl;
import cc.rengu.igas.bomp.common.entity.MchntBaseInfo;
import cc.rengu.igas.bomp.common.entity.TxnLimitCfg;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.TxnLimitCfgBean;
import cc.rengu.igas.bomp.facade.request.MchntTxnLimitCfgQueryRequest;
import cc.rengu.igas.bomp.facade.response.MchntTxnLimitCfgQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.enums.LimitTypeEnum;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * MchntTxnLimitCfg信息查询
 * Created by
 */
public class MchntTxnLimitCfgQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntTxnLimitCfgQueryRequest mchntTxnLimitCfgQueryRequest = new MchntTxnLimitCfgQueryRequest();
        ConvertUtil.convertOutput(mchntTxnLimitCfgQueryRequest);
        return mchntTxnLimitCfgQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntTxnLimitCfgQueryRequest mchntTxnLimitCfgQueryRequest = (MchntTxnLimitCfgQueryRequest) request;
        MchntTxnLimitCfgQueryResponse mchntTxnLimitCfgQueryResponse = new MchntTxnLimitCfgQueryResponse();
        BizResponse<MchntTxnLimitCfgQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.IGAS);
        BeanUtil.beanCopy(mchntTxnLimitCfgQueryRequest, mchntTxnLimitCfgQueryResponse);

        String instId = mchntTxnLimitCfgQueryRequest.getHeader().getInstId();
        String mchntNo = mchntTxnLimitCfgQueryRequest.getMchntNo();
        String termNo = mchntTxnLimitCfgQueryRequest.getTermNo();
        String txnNum = mchntTxnLimitCfgQueryRequest.getTxnNum();

        /* 查询表数据 */
        MchntBaseInfoMapper mchntBaseInfoMapper = new MchntBaseInfoMapperImpl();
        MchntBaseInfo mchntBaseInfo = mchntBaseInfoMapper.selectMchntBaseInfoByPrimaryKey(instId, mchntNo);
        if (null == mchntBaseInfo) {
            rglog.error("根据条件查询商户基本信息为空!");
            bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
            bizResponse.setResult(mchntTxnLimitCfgQueryResponse);
            return bizResponse;
        }

        String limitType;
        String limitInst;
        List<TxnLimitCfg> limitCfgs;
        TxnLimitCfgMapper txnLimitCfgMapper = new TxnLimitCfgMapperImpl();
        List<TxnLimitCfgBean> transLimitList = new ArrayList<>();        /* 交易限额配置信息 */
        if (!StringUtil.isEmptyOrNull(txnNum)) {
            limitType = LimitTypeEnum.MCHNT_TRANS.getLimitType();
            limitInst = mchntNo + "|" + txnNum;
            limitCfgs = txnLimitCfgMapper.selectTxnLimitCfgLimitTypeInst(instId, limitType, limitInst);
            if (null != limitCfgs && !limitCfgs.isEmpty()) {
                convTxnLimitCfg(limitCfgs, transLimitList);
            }
        }

        List<TxnLimitCfgBean> termLimitList = new ArrayList<>();         /* 终端限额配置信息 */
        if (!StringUtil.isEmptyOrNull(termNo)) {
            limitType = LimitTypeEnum.MCHNT_TERM.getLimitType();
            limitInst = mchntNo + "|" + termNo;
            limitCfgs = txnLimitCfgMapper.selectTxnLimitCfgLimitTypeInst(instId, limitType, limitInst);
            if (null != limitCfgs && !limitCfgs.isEmpty()) {
                convTxnLimitCfg(limitCfgs, termLimitList);
            }
        }

        List<TxnLimitCfgBean> mchntLimitList = new ArrayList<>();        /* 商户限额配置信息 */
        limitType = LimitTypeEnum.MCHNT_NO.getLimitType();
        limitInst = mchntNo;
        limitCfgs = txnLimitCfgMapper.selectTxnLimitCfgLimitTypeInst(instId, limitType, limitInst);
        if (null != limitCfgs && !limitCfgs.isEmpty()) {
            convTxnLimitCfg(limitCfgs, mchntLimitList);
        }

        List<TxnLimitCfgBean> mchntLevelLimitList = new ArrayList<>();   /* 商户等级限额配置信息 */
        if (!StringUtil.isEmptyOrNull(mchntBaseInfo.getMchntLevel())) {
            limitType = LimitTypeEnum.MCHNT_LEVEL.getLimitType();
            limitInst = mchntBaseInfo.getMchntLevel();
            limitCfgs = txnLimitCfgMapper.selectTxnLimitCfgLimitTypeInst(instId, limitType, limitInst);
            if (null != limitCfgs && !limitCfgs.isEmpty()) {
                convTxnLimitCfg(limitCfgs, mchntLevelLimitList);
            }
        }

        List<TxnLimitCfgBean> mchntMccLimitList = new ArrayList<>();     /* 行业类别限额配置信息 */
        if (!StringUtil.isEmptyOrNull(mchntBaseInfo.getIndustryType())) {
            limitType = LimitTypeEnum.MCC.getLimitType();
            limitInst = mchntBaseInfo.getIndustryType();
            limitCfgs = txnLimitCfgMapper.selectTxnLimitCfgLimitTypeInst(instId, limitType, limitInst);
            if (null != limitCfgs && !limitCfgs.isEmpty()) {
                convTxnLimitCfg(limitCfgs, mchntMccLimitList);
            }
        }

        List<TxnLimitCfgBean> mchntScopeLimitList = new ArrayList<>();   /* 行业组别限额配置信息 */
        if (!StringUtil.isEmptyOrNull(mchntBaseInfo.getManagementScope())) {
            limitType = LimitTypeEnum.MCHNT_SCOPE.getLimitType();
            limitInst = mchntBaseInfo.getManagementScope();
            limitCfgs = txnLimitCfgMapper.selectTxnLimitCfgLimitTypeInst(instId, limitType, limitInst);
            if (null != limitCfgs && !limitCfgs.isEmpty()) {
                convTxnLimitCfg(limitCfgs, mchntScopeLimitList);
            }
        }

        List<TxnLimitCfgBean> mchntRefundLimitList = new ArrayList<>();  /* 商户退款限额配置信息 */
        limitType = LimitTypeEnum.MCHNT_REFUND.getLimitType();
        limitInst = mchntNo;
        limitCfgs = txnLimitCfgMapper.selectTxnLimitCfgLimitTypeInst(instId, limitType, limitInst);
        if (null != limitCfgs && !limitCfgs.isEmpty()) {
            convTxnLimitCfg(limitCfgs, mchntRefundLimitList);
        }

        mchntTxnLimitCfgQueryResponse.setTransLimitList(transLimitList);
        mchntTxnLimitCfgQueryResponse.setTermLimitList(termLimitList);
        mchntTxnLimitCfgQueryResponse.setMchntLimitList(mchntLimitList);
        mchntTxnLimitCfgQueryResponse.setMchntLevelLimitList(mchntLevelLimitList);
        mchntTxnLimitCfgQueryResponse.setMchntMccLimitList(mchntMccLimitList);
        mchntTxnLimitCfgQueryResponse.setMchntScopeLimitList(mchntScopeLimitList);
        mchntTxnLimitCfgQueryResponse.setMchntRefundLimitList(mchntRefundLimitList);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntTxnLimitCfgQueryResponse);

        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntTxnLimitCfgQueryResponse mchntTxnLimitCfgQueryResponse = (MchntTxnLimitCfgQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntTxnLimitCfgQueryResponse);
    }


    private void convTxnLimitCfg(List<TxnLimitCfg> limitCfgs, List<TxnLimitCfgBean> transLimitList) {
        for (TxnLimitCfg limitCfg : limitCfgs) {
            TxnLimitCfgBean bean = new TxnLimitCfgBean();
            BeanUtil.beanCopy(limitCfg, bean);
            transLimitList.add(bean);
        }
    }
}
